const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

var log = wx.getRealtimeLogManager ? wx.getRealtimeLogManager() : null //log日志文件向微信后台发送
const logInfo = {
  debug() {
    if (!log) return
    log.debug.apply(log, arguments)
  },
  info() {
    if (!log) return
    log.info.apply(log, arguments)
  },
  warn() {
    if (!log) return
    log.warn.apply(log, arguments)
  },
  error() {
    if (!log) return
    log.error.apply(log, arguments)
  },
  setFilterMsg(msg) { // 从基础库2.7.3开始支持
    if (!log || !log.setFilterMsg) return
    if (typeof msg !== 'string') return
    log.setFilterMsg(msg)
  }
}

// 生成uuid
export function getUUID() {
	return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
		var r = Math.random() * 16 | 0,
			v = c == 'x' ? r : (r & 0x3 | 0x8);
		return v.toString(16);
	});
}

//根据身份证号识别出生日期 性别 年龄
function infoFromIdCard(userIdCard, num) {
  if (num == 1) {
    //获取出生日期
    var birth = userIdCard.substring(6, 10) + '-' + userIdCard.substring(10, 12) + '-' + userIdCard.substring(12, 14)
    return birth
  }
  if (num == 2) {
    //获取性别
    if (parseInt(userIdCard.substr(16, 1)) % 2 == 1) {
      //男
      return 1
    } else {
      //女
      return 0
    }
  }
  if (num == 3) {
    //获取年龄
    var myDate = new Date()
    var month = myDate.getMonth() + 1
    var day = myDate.getDate()
    var age = myDate.getFullYear() - userIdCard.substring(6, 10) - 1
    if (userIdCard.substring(10, 12) < month || userIdCard.substring(10, 12) == month && userIdCard.substring(12, 14) <= day) {
      age ++
    }
    return age
  }
}

function lineBreakFilter(text) {
  if(text){
    // console.log(text);
    var pattern = "\\\\n";
    var target = "\n";
    var reg = new RegExp(pattern, "g");
    return text.replace(reg, target);
  }
}


module.exports = {
  infoFromIdCard,
  formatTime,
  getUUID,
  logInfo,
  lineBreakFilter
}
